<template>
  <div>
    <el-card class="!border-none relative el-card-client" shadow="never">
      <template #header>
        <el-row :gutter="10">
          <el-col :span="3">
            <el-input
              class="input_style"
              v-model="queryParams.account"
              clearable
              placeholder="请输入用户名昵称或账号"
            />
          </el-col>
          <el-col :span="3">
            <el-input
              class="input_style"
              v-model="queryParams.nickname"
              clearable
              placeholder="请输入渠道来源名称"
            />
          </el-col>
          <el-col :span="3">
            <el-select
              class="input_style"
              v-model="queryParams.type"
              clearable
              placeholder="请选择类型"
            >
              <el-option
                v-for="(item, index) in dictData.type"
                :key="index"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-col>
          <el-col :span="3">
            <el-select
              class="input_style"
              v-model="queryParams.source"
              clearable
              placeholder="请选择来源"
            >
              <el-option
                v-for="(item, index) in dictData.source"
                :key="index"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-col>
          <el-col :span="10">
            <el-button
              @click="getLists"
              color="#63ccba"
              type="primary"
              class="btn_style mr_10"
            >
              <template #icon>
                <icon name="el-icon-Search" />
              </template>
              搜索
            </el-button>
            <el-button @click="getLists" plain class="mr_10">
              <template #icon>
                <icon name="el-icon-Refresh" />
              </template>
              重置
            </el-button>
          </el-col>
          <!-- <el-col :span="2" class="flex justify-end">
            <move-button></move-button>
          </el-col> -->
        </el-row>
      </template>
      <div>
        <el-table
          :data="pager.lists"
          border
          v-loading="pager.loading"
          stripe
          height="calc(100vh - 15rem)"
        >
          <el-table-column
            label="账户名称"
            prop="role_name"
            show-overflow-tooltip
          />
          <el-table-column label="类型" prop="status" show-overflow-tooltip />
          <el-table-column
            label="昵称"
            prop="role_name"
            show-overflow-tooltip
          />
          <el-table-column
            label="来源"
            prop="ac_source"
            show-overflow-tooltip
          />
          <el-table-column
            label="渠道来源"
            prop="comefrom"
            show-overflow-tooltip
          />
          <el-table-column
            label="登录时间"
            prop="login_time"
            show-overflow-tooltip
          />
          <el-table-column
            label="注册时间"
            prop="create_time"
            show-overflow-tooltip
          />
          <el-table-column label="ip" prop="ip" show-overflow-tooltip />
          <el-table-column
            label="ip区域"
            prop="ip_address"
            show-overflow-tooltip
          />
          <el-table-column label="状态">
            <template #default="{ row }">
              <el-switch
                v-model="row.status"
                @change="stateChangeSave(row)"
                :active-value="1"
                :inactive-value="0"
              />
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark" show-overflow-tooltip>
            <template #default="{ row }">
              <el-button type="info" link @click="openRemark(row)">{{
                row.remark ? row.remark : "未编辑"
              }}</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="flex">
        <pagination v-model="pager" @change="getLists" class="position" />
      </div>
    </el-card>
    <edit-popup
      v-if="showEdit"
      ref="editRef"
      :dict-data="dictData"
      @success="getLists"
      @close="showEdit = false"
    />
    <el-dialog v-model="showRemark" :title="dialogTitle" width="30%" center>
      <el-form ref="formRef" :model="formData" label-width="50px">
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="formData.remark"
            :rows="5"
            type="textarea"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showRemark = false">取消</el-button>
          <el-button type="primary" @click="submitRemark">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup name="zgOnlineLists">
import { usePaging } from "@/hooks/usePaging";
import { useDictData } from "@/hooks/useDictOptions";
import { apiZgOnlineLists, apiZgOnlineDelete } from "@/api/zg_online";
import {
  apiMemberLists,
  apiMemberDelete,
  apiMemberStatus,
  apiMemberDetail,
} from "@/api/member";
import { timeFormat } from "@/utils/util";
import feedback from "@/utils/feedback";
import EditPopup from "./edit.vue";
import moveButton from "@/components/moveButton/index.vue";

const editRef = shallowRef<InstanceType<typeof EditPopup>>();
// 是否显示编辑框
const showEdit = ref(false);
//编辑备注
const dialogTitle = ref<string>("");
const formData = reactive({
  id: "",
  remark: "",
});
// 是否显示备注编辑框
const showRemark = ref(false);
const submitRemark = async () => {
  await apiMemberStatus(formData);
  formData.remark = "";
  showRemark.value = false;
  getLists();
};
const openRemark = async (data: any) => {
  dialogTitle.value = "编辑备注";
  showRemark.value = true;
  formData.id = data.id;
  formData.remark = data.remark;
};
//修改状态
const stateChangeSave = async (data: any) => {
  await nextTick();
  await apiMemberStatus({
    id: data.id,
    status: data.status ? 1 : 0,
  });
  getLists();
};
// 查询条件
const queryParams = reactive({
  title: "",
});

// 选中数据
const selectData = ref<any[]>([]);

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
  selectData.value = val.map(({ id }) => id);
};

// 获取字典数据
const { dictData } = useDictData("");

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
  fetchFun: apiZgOnlineLists,
  params: queryParams,
});

// 添加
const handleAdd = async () => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("add");
};

// 编辑
const handleEdit = async (data: any) => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("edit");
  editRef.value?.setFormData(data);
};

// 删除
const handleDelete = async (id: number | any[]) => {
  await feedback.confirm("确定要删除？");
  await apiZgOnlineDelete({ id });
  getLists();
};

getLists();
</script>
<style lang="scss" scoped>
.flex {
  display: flex !important;
}

.mr_10 {
  margin-right: 0.63rem;
}

.btn_style {
  color: #fff;
}
</style>
